package com.wtw.udf;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.json.JSONObject;

public class BaseFieldUDF extends UDF {


    /**
     1583776223469|{
         "cm": {
             "ln": "-48.5",
             "sv": "V2.5.7",
             "os": "8.0.9",
             "g": "6F76AVD5@gmail.com",
             "mid": "0",
             "nw": "4G",
             "l": "pt",
             "vc": "3",
             "hw": "750*1134",
             "ar": "MX",
             "uid": "0",
             "t": "1583707297317",
             "la": "-52.9",
             "md": "sumsung-18",
             "vn": "1.2.4",
             "ba": "Sumsung",
             "sr": "V"
         },
         "ap": "app",
         "et": [{
                 "ett": "1583705574227",
                 "en": "display",
                 "kv": {
                     "goodsid": "0",
                     "action": "1",
                     "extend1": "1",
                     "place": "0",
                     "category": "63"
                 }
             },
             {
                 "ett": "1583760986259",
                 "en": "loading",
                 "kv": {
                     "extend2": "",
                     "loading_time": "4",
                     "action": "3",
                     "extend1": "",
                     "type": "3",
                     "type1": "",
                     "loading_way": "1"
                 }
             },
             {
                 "ett": "1583746639124",
                 "en": "ad",
                 "kv": {
                     "activityId": "1",
                     "displayMills": "111839",
                     "entry": "1",
                     "action": "5",
                     "contentType": "0"
                 }
             },
             {
                 "ett": "1583758016208",
                 "en": "notification",
                 "kv": {
                     "ap_time": "1583694079866",
                     "action": "1",
                     "type": "3",
                     "content": ""
                 }
             },
             {
                 "ett": "1583699890760",
                 "en": "favorites",
                 "kv": {
                     "course_id": 4,
                     "id": 0,
                     "add_time": "1583730648134",
                     "userid": 7
                 }
             }
         ]
     }
     */

    /**
     *
     * @param line  要处理的json字符串
     * @param key   要从传入的json中提取的字段
     * @return      返回提取出的字段的值
     */
    public String evaluate(String line, String key) {

        String[] log = line.split("\\|");

        // 判断log是否合法
        if(log.length != 2 || StringUtils.isBlank(log[1].trim())) {
            return "";
        }

        JSONObject jsonObject = new JSONObject(log[1].trim());
        String res = "";

        // 根据传进来的key取值
        // 传入st就返回服务器时间戳
        if(key.equals("st")) {
            res = log[0];
        } else if(key.equals("et")) {   // 传入et返回对应json字段的值

            if(jsonObject.has("et")) {
                res = jsonObject.getString("et");
            }

        } else {    // 返回cm对应json字段的值
            JSONObject cm = jsonObject.getJSONObject("cm");

            if(cm.has(key)) {
                res = cm.getString(key);
            }

        }

        return res;
    }

    public static void main(String[] args) {
        String line = "1583823937654|{\"cm\":{\"ln\":\"-90.2\",\"sv\":\"V2.6.3\",\"os\":\"8.2.0\",\"g\":\"ICMOAC5M@gmail.com\",\"mid\":\"0\",\"nw\":\"4G\",\"l\":\"es\",\"vc\":\"17\",\"hw\":\"640*1136\",\"ar\":\"MX\",\"uid\":\"0\",\"t\":\"1583734734518\",\"la\":\"14.5\",\"md\":\"sumsung-16\",\"vn\":\"1.3.4\",\"ba\":\"Sumsung\",\"sr\":\"A\"},\"ap\":\"app\",\"et\":[{\"ett\":\"1583726726627\",\"en\":\"display\",\"kv\":{\"goodsid\":\"0\",\"action\":\"2\",\"extend1\":\"2\",\"place\":\"0\",\"category\":\"86\"}},{\"ett\":\"1583822064016\",\"en\":\"active_background\",\"kv\":{\"active_source\":\"3\"}},{\"ett\":\"1583744691722\",\"en\":\"comment\",\"kv\":{\"p_comment_id\":0,\"addtime\":\"1583753543284\",\"praise_count\":762,\"other_id\":9,\"comment_id\":2,\"reply_count\":29,\"userid\":1,\"content\":\"箔溶蟹\"}}]}";
        String res = new BaseFieldUDF().evaluate(line, "st");

        System.out.println(res);
    }
}
